

import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { getToken, getUserStatus } from "@/utils/auth"; // get token from cookie

import { apiConfigCurrent } from "@/api/apiCommon"; // get token from cookie
import { setStorageSync, getStorageSync } from '@/utils/storageUtils'

import getPageTitle from "@/utils/get-page-title";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = ["/login"]; // no redirect whitelist

var init_user_status = false;

apiConfigCurrent({}).then((response) => {
  var config_current = response.data;
  setStorageSync('config_current', config_current);
})

router.beforeEach(async (to, from, next) => {

  // start progress bar
  NProgress.start();

  // set page title
  document.title = getPageTitle(to.meta.title);

  const roles = to.meta.roles;


  const user_status = getUserStatus();
  // if (init_user_status == false) {
  //   console.log("初始化用户")
  //   await store.dispatch('user/init_userinof');
  //   // console.log()
  //   // router.addRoutes()
  //   init_user_status = true;
  // }


  if (user_status) {
    if (to.path === "/login") {
      NProgress.done();
      next('/');
    } else if (to.path == '\\404') {
      next('\\');
      NProgress.done();
    } else {
      // console.log(to.path, '用户已经初始化 ,跳转该页面')
      NProgress.done();
      next();
    }
  } else {
    // console.log("登录过期 跳转登录页", [to.path])

    if (whiteList.indexOf(to.path) !== -1) {

      next();
      NProgress.done();
    } else if (to.path == '\\404') {
      next('\\');
      NProgress.done();
    } else {
      NProgress.done();
      next(`/login`);
    }
  }
});

router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});
